package org.springblade.modules.platform.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.AllArgsConstructor;
import org.springblade.core.redis.cache.BladeRedis;
import org.springblade.modules.platform.dto.PhysicalDataTableDTO;
import org.springblade.modules.platform.dto.PhysicalDataTableListByPageDTO;
import org.springblade.modules.platform.entity.AbsolutelyValueTable;
import org.springblade.modules.platform.entity.MeasuringChainPlatformType;
import org.springblade.modules.platform.entity.MeasuringPlatformType;
import org.springblade.modules.platform.entity.PhysicalValueTable;
import org.springblade.modules.platform.mapper.AbsolutelyValueTableMapper;
import org.springblade.modules.platform.mapper.MeasuringChainPlatformTypeMapper;
import org.springblade.modules.platform.mapper.MeasuringPlatformTypeMapper;
import org.springblade.modules.platform.mapper.PhysicalValueTableMapper;
import org.springblade.modules.platform.service.IPhysicalValueTableService;

import org.springblade.modules.platform.utils.TimeUtils;
import org.springblade.modules.platform.vo.*;
import org.springblade.core.tool.utils.DateUtil;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.*;
import java.util.stream.Collectors;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author SYG
 * @since 2022-12-08
 */
@Service
@AllArgsConstructor
public class PhysicalValueTableServiceImpl extends ServiceImpl<PhysicalValueTableMapper, PhysicalValueTable> implements IPhysicalValueTableService {

    @Autowired
    AbsolutelyValueTableMapper absolutelyValueTableMapper;
	private final BladeRedis bladeRedis;
	private final MeasuringPlatformTypeMapper measuringPlatformTypeMapper;
    @Override
    public PhysicalValuePageVO getListByPage(PhysicalDataTableListByPageDTO physicalDataTableListByPageDTO) {

        PhysicalValuePageVO physicalValuePageVO = new PhysicalValuePageVO();
        QueryWrapper<PhysicalValueTable> queryWrapper = new QueryWrapper<>();

        if(physicalDataTableListByPageDTO.getEndTime()!=null&&physicalDataTableListByPageDTO.getStartTime()!=null){
            queryWrapper.lambda().ge(PhysicalValueTable::getGatherTime,physicalDataTableListByPageDTO.getStartTime()).le(PhysicalValueTable::getGatherTime,physicalDataTableListByPageDTO.getEndTime());
        }
        if(physicalDataTableListByPageDTO.getGatherEvent()!=null){
            queryWrapper.lambda().eq(PhysicalValueTable::getGatherEvent,physicalDataTableListByPageDTO.getGatherEvent());
        }
        if(physicalDataTableListByPageDTO.getMeasurChain()!=null){
            queryWrapper.lambda().like(PhysicalValueTable::getMeasurChain,physicalDataTableListByPageDTO.getMeasurChain());
        }
        queryWrapper.lambda().orderByDesc(PhysicalValueTable::getGatherTime)/*.orderByDesc(PhysicalValueTable::getId)*/;
        Page<PhysicalValueTable> rawDataTablePage = baseMapper.selectPage(new Page<>(physicalDataTableListByPageDTO.getCurrent(), physicalDataTableListByPageDTO.getSize()), queryWrapper);
        physicalValuePageVO.setTotal(rawDataTablePage.getTotal());
        if(rawDataTablePage.getTotal()>0){
            List<PhysicalValueTable> records = rawDataTablePage.getRecords();
            List<PhysicalValueTableVO> collect = records.stream().map(x -> {
                PhysicalValueTableVO physicalValueTableVO = new PhysicalValueTableVO();
                BeanUtils.copyProperties(x, physicalValueTableVO);
                physicalValueTableVO.setGatherEvent(x.getGatherEvent()==2?"手动采集"+x.getAcquisitionFlag():"定时采集");
                return physicalValueTableVO;
            }).collect(Collectors.toList());

            physicalValuePageVO.setList(collect.stream().sorted(Comparator.comparing(PhysicalValueTableVO::getId)).collect(Collectors.toList()));
        }
        return physicalValuePageVO;
    }

    @Override
    public DataDetailsVO.PhysicalValueTableVO selectOneByDeptId(Long deptId, Long gatherTime,Integer measurChainDirection) {
        PhysicalValueTable physicalValueTable = baseMapper.selectOne(Wrappers.<PhysicalValueTable>query().lambda()
                .eq(PhysicalValueTable::getDeptId, deptId)
                .eq(PhysicalValueTable::getMeasurChainDirection, measurChainDirection)
                .eq(PhysicalValueTable::getGatherTime, gatherTime));
        if(physicalValueTable != null){
            DataDetailsVO.PhysicalValueTableVO physicalValueTableVO = new DataDetailsVO.PhysicalValueTableVO();
            BeanUtils.copyProperties(physicalValueTable, physicalValueTableVO);
            return physicalValueTableVO;
        }
        return null;
    }

    @Override
    public PhysicalValueTableListVO getProductionChart(Long deptId, Integer measurChainDirection, Long startTime, Long endTime) {
        QueryWrapper<PhysicalValueTable> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(PhysicalValueTable::getDeptId,deptId).eq(PhysicalValueTable::getMeasurChainDirection,measurChainDirection);
        if(startTime!=null&&endTime!=null){
            Date date = TimeUtils.currentMax(new Date(endTime));
            Date date1 = TimeUtils.currentMin(new Date(startTime));
            queryWrapper.lambda().between(PhysicalValueTable::getGatherTime,date1.getTime(),date.getTime());
        }else {
            Date date = TimeUtils.currentMax(new Date());
            Date date1 =  TimeUtils.localDateTime2Date(TimeUtils.localDateTimeSubtractionDay(TimeUtils.date2LocalDateTime(new Date()),100));
            queryWrapper.lambda().between(PhysicalValueTable::getGatherTime,date1.getTime(),date.getTime());
        }
        queryWrapper.lambda().orderByAsc(PhysicalValueTable::getGatherTime);
        List<PhysicalValueTable> engineerValueTables = baseMapper.selectList(queryWrapper);
        if(CollectionUtils.isEmpty(engineerValueTables)){
            return null;
        }
        PhysicalValueTableListVO productionChartVO = new PhysicalValueTableListVO();
        List<PhysicalValueTableListVO.PhysicalValueTableVO> physicalValueTableVOS = new ArrayList<>();
        engineerValueTables.forEach(x->{
            PhysicalValueTableListVO.PhysicalValueTableVO deptBenchInfoVO = new PhysicalValueTableListVO.PhysicalValueTableVO();
            deptBenchInfoVO.setDeptId(x.getDeptId());
            deptBenchInfoVO.setDeptName(x.getDeptName());
            deptBenchInfoVO.setGatherTime(TimeUtils.timeStampConversionTime(x.getGatherTime(), DateUtil.PATTERN_DATETIME));
            deptBenchInfoVO.setDeltaY(x.getDeltaY());
			deptBenchInfoVO.setDeltaYname("铟钢管的相对变形量");
			deptBenchInfoVO.setDeltaYcompany("°");

            deptBenchInfoVO.setDipY(x.getDipY());
			deptBenchInfoVO.setDipYname("倾角Y");
			deptBenchInfoVO.setDipYcompany("rad");

            deptBenchInfoVO.setDipX(x.getDipX());
			deptBenchInfoVO.setDipXname("倾角X");
			deptBenchInfoVO.setDipXcompany("rad");

            deptBenchInfoVO.setNX(x.getnX());
			deptBenchInfoVO.setNXcompany("mm");
			deptBenchInfoVO.setNXname("逆向光束的X方向相对变形量");

            deptBenchInfoVO.setNZ(x.getnZ());
			deptBenchInfoVO.setNZcompany("mm");
			deptBenchInfoVO.setNZname("逆向光束的Z方向相对变形量");


            deptBenchInfoVO.setZX(x.getzX());
			deptBenchInfoVO.setZXcompany("mm");
			deptBenchInfoVO.setZXname("正向光束的X方向相对变形量");



            deptBenchInfoVO.setZZ(x.getzZ());
			deptBenchInfoVO.setZZcompany("mm");
			deptBenchInfoVO.setZZname("正向光束的Z方向相对变形量");

			deptBenchInfoVO.setX26(x.getX26());
			deptBenchInfoVO.setX26company("mm");
			deptBenchInfoVO.setX26name("PL-DB08-01-X");
			deptBenchInfoVO.setY25(x.getY25());
			deptBenchInfoVO.setY25company("mm");
			deptBenchInfoVO.setY25name("PL-DB08-01-Y");
			deptBenchInfoVO.setY27(x.getY27());
			deptBenchInfoVO.setY27company("mm");
			deptBenchInfoVO.setY27name("IP-DB08-01-Y");
			deptBenchInfoVO.setX28(x.getX28());
			deptBenchInfoVO.setX28company("mm");
			deptBenchInfoVO.setX28name("IP-DB08-01-X");
			deptBenchInfoVO.setX29(x.getX29());
			deptBenchInfoVO.setX29company("mm");
			deptBenchInfoVO.setX29name("PL-DB08-02-X");
			deptBenchInfoVO.setY30(x.getY30());
			deptBenchInfoVO.setY30company("mm");
			deptBenchInfoVO.setY30name("PL-DB08-02-Y");
			deptBenchInfoVO.setX32(x.getX32());
			deptBenchInfoVO.setX32company("mm");
			deptBenchInfoVO.setX32name("IP-DB08-01-Y");
			deptBenchInfoVO.setY31(x.getY31());
			deptBenchInfoVO.setY31company("mm");
			deptBenchInfoVO.setY31name("PL-DB08-03-Y");
			deptBenchInfoVO.setX33(x.getX33());
			deptBenchInfoVO.setX33company("mm");
			deptBenchInfoVO.setX33name("PL-DB08-03-X");
			deptBenchInfoVO.setY34(x.getY34());
			deptBenchInfoVO.setY34company("mm");
			deptBenchInfoVO.setY34name("PL-DB08-04-Y");




            physicalValueTableVOS.add(deptBenchInfoVO);
        });
        productionChartVO.setPhysicalValueTableVOS(physicalValueTableVOS);
        return productionChartVO;
    }

    @Override
    public void selectPhysical(PhysicalDataTableDTO physicalDataTableDTO) {
        List<PhysicalValueTable> physicalValueTables = baseMapper.selectList(Wrappers.<PhysicalValueTable>query().lambda()
                .eq(PhysicalValueTable::getGatherTime, physicalDataTableDTO.getGatherTime())
                .eq(PhysicalValueTable::getMeasurChainCode, physicalDataTableDTO.getMeasurChainCode()));
        if(!CollectionUtils.isEmpty(physicalValueTables)){
			Map<Integer, List<PhysicalValueTable>> collect = physicalValueTables.stream().collect(
				Collectors.groupingBy(PhysicalValueTable::getMeasurChainDirection, Collectors.toList()));
			collect.forEach((x,y)->{
				List<AbsolutelyValueTable> absolutelyValueTables = absolutelyValueTableMapper.selectList(Wrappers.<AbsolutelyValueTable>query().lambda()
					.eq(AbsolutelyValueTable::getMeasurChainDirection,x));
				y.forEach(l1->{
					Long deptId = l1.getDeptId();
					MeasuringPlatformType measuringPlatformType = measuringPlatformTypeMapper.selectById(deptId);

					AbsolutelyValueTable absolutelyValueTable = absolutelyValueTables.stream().filter(l -> l.getDeptId()!=null&&l.getDeptId().equals(measuringPlatformType.getDeviceId())).findFirst().get();
					absolutelyValueTable.setzX(l1.getzX());
					absolutelyValueTable.setzZ(l1.getzZ());
					absolutelyValueTable.setnX(l1.getnX());
					absolutelyValueTable.setnZ(l1.getnZ());
					absolutelyValueTable.setDipX(l1.getDipX());
					absolutelyValueTable.setDipY(l1.getDipY());
					absolutelyValueTable.setDeltaY(l1.getDeltaY());
					absolutelyValueTable.setY( l1.getDipY());
					absolutelyValueTable.setCcd2FrontDipY(0);
					absolutelyValueTable.setCcd2AfterDipY(0);
					absolutelyValueTable.setCcd1AfterDipX(l1.getCcd1AfterDipX());
					absolutelyValueTable.setCcd1FrontDipX(l1.getCcd1FrontDipX());
					absolutelyValueTable.setGatherTime(l1.getGatherTime());
					absolutelyValueTableMapper.updateById(absolutelyValueTable);
				});
/*				bladeRedis.set("device:measuringChain"+x,bladeRedis.get("device:measuringChain:beifen"+x));*/
			});




        }


    }
}
